//
//  DFProductCell.swift
//  DailyFresh
//
//  Created by Arthur on 2017/9/20.
//  Copyright © 2017年 DailyFresh. All rights reserved.
//

import UIKit

let DFProductCellHeight = normalize(value: 165)
let DFProductCellImageHeight = normalize(value: 115)

class DFProductCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(image)
        self.addSubview(tagIcon)
        self.addSubview(name)
        self.addSubview(subTitle)
        self.addSubview(priceUp)
        self.addSubview(priceDown)
        self.addSubview(line)
        self.addSubview(addShoppingCartButton)
        self.addSubview(tagsWrapView)

        tagIcon.snp.makeConstraints { (make) in
            make.left.equalTo(DFLayoutConstants.margin)
            make.size.equalTo(CGSize.init(width: normalize(value: 24), height: 32))
            make.top.equalTo(normalize(value: 20))
        }
        
        image.snp.makeConstraints { (make) in
            make.left.equalTo(normalize(value: 30))
            make.size.equalTo(CGSize.init(width: normalize(value: DFProductCellImageHeight), height: DFProductCellImageHeight))
            make.centerY.equalTo(self)
        }
        
        name.snp.makeConstraints { (make) in
            make.left.equalTo(image.snp.right).offset(normalize(value: 20))
            make.right.equalTo(-DFLayoutConstants.margin)
            make.top.equalTo(normalize(value: 30))
            make.height.equalTo(normalize(value: 16))
        }
        
        subTitle.snp.makeConstraints { (make) in
            make.left.equalTo(image.snp.right).offset(normalize(value: 20))
            make.right.equalTo(-DFLayoutConstants.margin)
            make.top.equalTo(name.snp.bottom).offset(normalize(value: 3))
            make.height.equalTo(normalize(value: 15))
        }
        

        priceDown.snp.makeConstraints { (make) in
            make.left.equalTo(image.snp.right).offset(normalize(value: 20))
            make.right.equalTo(-DFLayoutConstants.margin)
            make.bottom.equalTo(-normalize(value: 30))
            make.height.equalTo(normalize(value: 13))
        }
        
        priceUp.snp.makeConstraints { (make) in
            make.left.equalTo(image.snp.right).offset(normalize(value: 20))
            make.right.equalTo(-DFLayoutConstants.margin)
            make.bottom.equalTo(priceDown.snp.top).offset(-normalize(value: 3))
            make.height.equalTo(normalize(value: 12))
        }
        
        addShoppingCartButton.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.init(width: normalize(value: 28), height: 28))
            make.bottom.equalTo(-normalize(value: 30))
            make.right.equalTo(-DFLayoutConstants.margin)

        }
        
        tagsWrapView.snp.makeConstraints { (make) in
            make.left.equalTo(image.snp.right).offset(normalize(value: 20))
            make.right.equalTo(-DFLayoutConstants.margin)
            make.top.equalTo(subTitle.snp.bottom).offset(normalize(value: 5))
            make.height.equalTo(DFProductTagHeight)
        }
        
        line.snp.makeConstraints { (make) in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(self.height-0.5, DFLayoutConstants.margin, 0, 0))
        }
        
        
    }
    
    public func updateCell(product: DFProductModel) {
//        tagIcon.setImageWith(URL.init(string: product.promote_tag), options: YYWebImageOptions.)\
        tagIcon.setImageWith(URL.init(string: product.promote_tag), placeholder: nil)
        image.setImageWith(URL.init(string: product.image), placeholder: nil)
        name.text = product.name
        subTitle.text = product.subtitle
        
        let priceupF: Float = Float((product.priceUp?.price)!)/100.0
        priceUp.text = (product.priceUp?.name)! + "￥" + String(format: "%0.2f", priceupF)
        
        let pricedownF: Float = Float((product.priceDown?.price)!)/100.0
        priceDown.text = (product.priceDown?.name)! + "￥" + String(format: "%0.2f", pricedownF)
        
        if isArrayEmpty(product.product_tags) {
            tagsWrapView.isHidden = true
        } else {
            tagsWrapView.isHidden = false
            tagsWrapView.update(tags: product.product_tags)
        }

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var tagIcon: UIImageView = {
        let tmp = UIImageView()
        
        return tmp
    }()
    
    lazy var image: UIImageView = {
        let tmp = UIImageView()
        
        return tmp
    }()
    
    lazy var name: UILabel = {
        let tmp = UILabel()
        tmp.textColor = DFColorConstants.black
        tmp.numberOfLines = 1
        tmp.font = UIFont.systemFont(ofSize: 16)
        return tmp
    }()
    
    lazy var subTitle: UILabel = {
        let tmp = UILabel()
        tmp.numberOfLines = 1
        tmp.textColor = DFColorConstants.gray
        tmp.font = UIFont.systemFont(ofSize: 14)
        return tmp
    }()
    
    lazy var priceUp: UILabel = {
        let tmp = UILabel()
        tmp.numberOfLines = 1
        tmp.textColor = DFColorConstants.gray
        tmp.font = UIFont.systemFont(ofSize: 14)
        return tmp
    }()
    
    lazy var priceDown: UILabel = {
        let tmp = UILabel()
        tmp.textColor = DFColorConstants.main
        tmp.numberOfLines = 1
        tmp.font = UIFont.systemFont(ofSize: 15)
        return tmp
    }()
    
    lazy var line: UIView = {
        let tmp = UIView()
        tmp.backgroundColor = DFColorConstants.line
        return tmp
    }()
    
    //newHomeRecommendShopCartImage_28x28_
    lazy var addShoppingCartButton: UIButton = {
        let tmp = UIButton.init(type: .custom)
        tmp.setImage(#imageLiteral(resourceName: "newHomeRecommendShopCartImage_28x28_"), for: .normal)
        return tmp
    }()
    
    lazy var tagsWrapView: DFProductTagWrapView = {
        let tmp = DFProductTagWrapView()
        return tmp
    }()
    
}
